package workbench.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import settings.domain.User;
import settings.service.UserService;
import util.DateUtil;
import util.JsonUtil;
import util.UUIDUtil;
import workbench.dao.ActivityDao;
import workbench.domain.Activity;
import workbench.domain.Clue;
import workbench.domain.Tran;
import workbench.exception.ConvertException;
import workbench.exception.DeleteException;
import workbench.exception.InsertClueException;
import workbench.service.ActivityService;
import workbench.service.ClueService;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityService activityService;
    @RequestMapping(value = "/getUserList")
    @ResponseBody
    public String getUserList() throws JsonProcessingException {
        List<User> userList= userService.getAllUser();
        String json= JsonUtil.writeValueAsString(userList);
        return json;
    }
    @RequestMapping(value = "/addClue",method = RequestMethod.POST)
    @ResponseBody
    public String addClue(Clue clue, HttpServletRequest req) throws InsertClueException {
        clue.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        clue.setCreateBy(((User)req.getSession().getAttribute("userMessage")).getName());
        clue.setId(UUIDUtil.getUUID());
        clueService.addClue(clue);
        String json= JsonUtil.ordinaryValueToJson("success","true");
        return json;
    }
    @RequestMapping(value = "/goClueDetail")
    public ModelAndView goClueDetail(String id) throws Exception {
        Clue clue=clueService.getClueById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("clueMessage",clue);
        modelAndView.setViewName("clueDetail");
        return modelAndView;
    }
    @RequestMapping(value = "/getActivityById")
    @ResponseBody
    public String getActivityById(String id) throws JsonProcessingException {
        List<Activity> list=clueService.getActivityById(id);
        String json= JsonUtil.writeValueAsString(list);
        return json;
    }
    @RequestMapping(value = "/unbind",method = RequestMethod.POST)
    @ResponseBody
    public String unbind(String aId,String cId) throws DeleteException {
        clueService.unbind(aId, cId);
        String json= JsonUtil.ordinaryValueToJson("success","true");
        return json;
    }
    @RequestMapping(value = "/getNotBindActivityListByName")
    @ResponseBody
    public String getNotBindActivityListByName(String aname,String clueId) throws JsonProcessingException {
        List<Activity> list=clueService.getNotBindActivityListByName(aname,clueId);
        String json= JsonUtil.writeValueAsString(list);
        return json;
    }
    @RequestMapping(value = "/bind",method = RequestMethod.POST)
    @ResponseBody
    public String bind(String cId,String[] aIds) throws Exception {
        clueService.bind(aIds, cId);
        String json=JsonUtil.ordinaryValueToJson("success","true");
        return json;
    }
    @RequestMapping(value = "/getActivityListByName")
    @ResponseBody
    public String getActivityListByName(String aname) throws Exception {
        List<Activity> list = clueService.getActivityListByName(aname);
        String json=JsonUtil.writeValueAsString(list);
        return json;
    }
    @RequestMapping(value = "/convert",method = RequestMethod.POST)
    public ModelAndView convert(boolean flag, String clueId, Tran tran,HttpServletRequest request) throws ConvertException {
        clueService.convert(flag, clueId, tran,((User)request.getSession().getAttribute("userMessage")).getName());
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:/static/workbench/clue/index.jsp");
        return modelAndView;
    }
}
